package com.bjtu.literatureclient.service.category.impl;

import com.bjtu.literatureclient.dao.category.ICategoryDao;
import com.bjtu.literatureclient.dao.journal.IJournalDao;
import com.bjtu.literatureclient.dao.user.UserIndexDao;
import com.bjtu.literatureclient.domain.base.BaseModel;
import com.bjtu.literatureclient.domain.category.SubsetIndexNo;
import com.bjtu.literatureclient.domain.user.UserIndexNoTable;
import com.bjtu.literatureclient.service.category.ICategorySrevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
*@author LMR
*@data 2018年1月30日下午8:39:09
*/
@Service("ICategorySrevice")
public class CategoryServiceImpl implements ICategorySrevice {

	@Autowired
    private ICategoryDao categoryDao;

	@Autowired
	private UserIndexDao userIndexDao;

	@Autowired
	private IJournalDao journalDao;
	
	@Override
	public List<SubsetIndexNo> queryCategory(String userNo) {
		// TODO Auto-generated method stub
		return this.categoryDao.queryCategory(userNo);
	}

	@Override
	public List<SubsetIndexNo> queryMajor(BaseModel model) {
		// TODO Auto-generated method stub
		return this.categoryDao.queryMajor(model);
	}

	@Override
	public String queryNewSubsetNo(String userNo) {
		List<SubsetIndexNo> subset = categoryDao.querySubsetByUser(userNo);
		if(subset!=null){
			List<Integer> substeNos = this.categoryDao.queryNewSubsetNo(userNo);
			int no =1;
			while(substeNos.contains(no)){
				no +=1;
			}

			return Integer.toString(no);
		}else{
			return "1";
		}
	}

	@Override
	public int addNewCategory(String userNo, String subsetNo, String subsetTitle) {
		List<String> title =this.categoryDao.queryTitleByUserNo(userNo);
		if(title.contains(subsetTitle)){
			return 999;
		}
		Date d=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//格式化为日期/时间字符串
		String subsetDate =sdf.format(new Date());
		//TODO : indexNo 是 userIndexNo 的 indexNo外键 <-(不满足)  UserNo 是 user表的 UserNo 的外键
		String indexNo = "0";
		UserIndexNoTable userIndexNoTable =new UserIndexNoTable();
		userIndexNoTable.setIndexNo("0");
		userIndexNoTable.setUserNo(userNo);
		if(userIndexDao.queryByuserAndindex(userIndexNoTable.getUserNo(),userIndexNoTable.getIndexNo())==null){
			userIndexDao.addUserIndexNoTable(userIndexNoTable);
		}
		return this.categoryDao.addNewCategory(userNo, indexNo ,subsetNo, subsetTitle,subsetDate);
	}

	@Override
	public List<UserIndexNoTable> queryBySet(String userNo, String subsetNo) {
		List<UserIndexNoTable> subsets=new ArrayList<UserIndexNoTable>();
		List<String> indexs = categoryDao.queryBySubSetNo(userNo,subsetNo);
		for (String indexNo :indexs){
			if(!indexNo.equals("0")){
				UserIndexNoTable userindex=userIndexDao.queryByuserAndindex(userNo,indexNo);
				subsets.add(userindex);
			}
//			if(userindex!=null){
//				if(userindex.getIndexCode().equals("J")&&userindex.getStatus()!=3){
//					JournalPaperChinese journalPaperChinese =journalDao.queryJournalPaperChinese(indexNo);
//					if(journalPaperChinese!=null){
//						vector.add(journalPaperChinese);
//					}else{
//						JournalPaperEnglish journalPaperEnglish =journalDao.queryJournalPaperEnglish(indexNo);
//						if(journalPaperEnglish!=null){
//							vector.add(journalPaperEnglish);;
//						}
//					}
//				}
//			}
		}
		return subsets;
	}

	@Override
	public boolean deleteCategory(String userNo, String subsetNo) {
		userIndexDao.deleteUserIndexNoTable(userNo,"0");
		return categoryDao.deleteCategory( userNo,  subsetNo);
	}

	@Override
	public List<UserIndexNoTable> queryAllreference(String userNo) {

		return this.userIndexDao.queryAllreference(userNo);
	}

	@Override
	public boolean addPapersToGroup(List<SubsetIndexNo> subsets) {
		boolean bo = false;
		List<String> title =this.categoryDao.querySubsetTitle(subsets.get(0).getUserNo(),subsets.get(0).getSubsetNo());
		for (SubsetIndexNo subset :subsets){
			subset.setSubsetTitle(title.get(0));
			int i = categoryDao.addNewCategory(subset.getUserNo(),subset.getIndexNo(),subset.getSubsetNo(),subset.getSubsetTitle(),subset.getSubsetDate());
			if(i<0){
				bo =false;
				break;
			}
		}
		return bo;
	}

	@Override
	public boolean deletePaperFromGroup(String userNo, String indexNo, String subsetNo) {
		return this.categoryDao.deletePaperFromGroup(userNo, indexNo, subsetNo);
	}

}
